#include <iostream>
#include <string>
#include <map>
using namespace std;
//string s[100002];

string solve(string s)
{
	string ans="";
	ans=ans+s[0];
	for(int i=1;i<s.size();i++)
	{
		if(s[i]>=97&&s[i]<=122&&s[i-1]==' ')
		{
			ans=ans+s[i];
		}
	}
	return ans;
}

int main()
{
	int n;
	cin >> n;
	map <string,string> b;
    for(int i=0;i<=n;i++)
    {
    	string s;
    	getline(cin,s);
    	string s1=solve(s);
    	b[s]=s1;
	}
	int m;
	while(m--)
	{
		string a;
		getline(cin,a);
		getline(cin,a);
		string a1=solve(a);
		int count=0;
		for(map<string,string>::iterator it=b.begin();it!=b.end();it++)
		{
			if(it->second==a1)
			{
				count++;
			}
		}
		if(count==0)
		{
			cout << a << endl;
		}
		else if(count==1)
		{
			for(map<string,string>::iterator it=b.begin();it!=b.end();it++)
		    {
		 	    if(it->second==a1)
			    {
				    cout << it->first << endl;
			    }
		    }
		}
		else if(count>=2)
		{
			int count1=0;
			for(map<string,string>::iterator it=b.begin();it!=b.end();it++)
		    {
		 	    if(it->second==a1&&count1<=count-2)
			    {
				    cout << it->first << "|";
				    count1++;
			    }
			    else if(it->second==a1&&count1==count-1)
			    {
			    	cout << it->first << endl;
				}
		    }
		}
	}
	return 0;
}
